package com.roy.udp;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

/**
 * @author ：楼兰
 * @date ：Created in 2021/8/6
 * @description:
 **/

public class UDPServer {
    public static void main(String[] args) throws IOException {
        final DatagramChannel channel = DatagramChannel.open();
        channel.bind(new InetSocketAddress(9999));

        final ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (true){
            System.out.println("=================");
            buffer.clear();
            channel.receive(buffer);
            String message = new String(buffer.array());
            System.out.println("received from client "+message);
        }
    }
}
